<?php
namespace Common\Controller;
/**
 * 文件上传控制器
 *
 * @since 1.0 2014-11-7 by sutroon
 */
class UploadHandlerController extends CommonController {

    /**
     * kindeditor编辑器上传处理
     * @since 1.0 2014-11-7 by sutroon
     */
    public function kindeupload() {
        $msg = self::uploadToSite($this->loginData['siteID'], 'KINDUPLOAD');
        $result = $msg['success'] ? array('error' => 0, 'url' => $msg['message']) : array('error' => 1, 'message' => $msg['message']);
        $this->ajaxReturn($result);
    }

    /**
     * 上传文件到租户上传目录中
     * @param int $siteID 租户ID
     * @param string $exTags 系统标签 如：PRODUCT-THUMB, USER-FACE
     * @param array $options 上传选项 array('subFolder' => 'Images', 'thumb' => false, 'subType' => 'date');
     * @param int $userID 用户编号
     * @return array array('success'=>bool, 'message'=>error|filePath, 'data'=>data); data包含fileName,filePath,fileExt,fileSize,fileID
     * @since 1.0 2014-12-27 by sutroon
     * @example
     *  ==上传商品==
     *  $msg=UploadHandlerAction::uploadToSite($siteID, 'PRODUCT-THUMB');
     *  if($msg['success']){
     *      $params->addData('thumbUrl', $msg['message']);
     *  }
     *  ==上传头像==
     *  $msg=UploadHandlerAction::uploadToSite($siteID, 'USER-FACE',array('subFolder'=>'Face'));
     *  ==上传Excel==
     *  $msg=UploadHandlerAction::uploadToSite($siteID, 'PRODUCT-IMPORT',array('subFolder'=>'ProductExcel')); // 导入商品数据
     *  $msg = UploadHandlerAction::uploadToSite($siteID, 'CUSTOMER-IMPORT', array('subFolder' => 'CustomerExcel'), $userID, array('remark' => "$skipDuplicate,$rule")); // 导入客户资料
     */
    public static function uploadToSite($siteID, $exTags, $options = array(), $userID = 0, $exData = array()) {
        $opts = array('subFolder' => 'Images', 'thumb' => false, 'subType' => 'date', 'exData' => array('exTags' => $exTags, 'siteID' => $siteID, 'userID' => $userID));
        if ($options) {
            $opts = array_merge($opts, $options);
        }
        $exData = array_merge(array('exTags' => $exTags, 'siteID' => $siteID, 'userID' => $userID), $exData);
        $msg = sofunc_upload_handle(UPLOAD_ROOT, array('savePath' => 'site_' . $siteID . '/' . $opts['subFolder'] . '/', 'autoSub' => true, 'subType' => $opts['subType'], 'thumb' => $opts['thumb'], 'exData' => $exData));
        $result = false;
        if (is_array($msg)) {
            $result = array('success' => true, 'message' => '/Upload/' . $msg['filePath'], 'data' => $msg);
        } else {
            $result = array('success' => false, 'message' => $msg);
        }
        return $result;
    }

    /**
     * 上传语音文件到Asterisk
     * @param int $siteID 租户ID
     * @param string $type 只支持moh、sounds
     * @param string $dirname 子目录名称
     * @param string $fieldName 上传域名称 如：fileUrl
     * @return array array('success'=>bool,'message'=>error|fileName, 'data'=>array);
     * @since 1.0 2014-12-27 by sutroon
     */
    public static function uploadToAsterisk($siteID, $type, $dirname, $fieldName = 'fileUrl') {
        if ($type == 'moh') {
            $dir = '/var/lib/asterisk/moh';
            $allowExts = array('mp3', 'wav');
        } else {
            $dir = '/var/lib/asterisk/sounds';
            $allowExts = array('g729', 'mp3', 'wav');
        }
        if (!sofunc_start_with($dirname, 'xcall_')) {
            $dirname = 'xcall_' . $siteID . '_' . $dirname;
        }
        $dir .= '/' . $dirname . '/';
        if ($type == 'moh' && is_dir($dir)) {
            return array('success' => false, 'message' => '子目录已存在!');
        }

        if (isset($_FILES[$fieldName]['name'])) {
            return array('success' => false, 'message' => '未选择文件!');
        }
        $filename = $_FILES[$fieldName]['name'];
        if (strpos($filename, ' ') !== false) {
            return array('success' => false, 'message' => '文件名不能包含空格!');
        }
        $ext = substr($filename, strrpos($filename, '.') + 1);
        $fnotext = substr($filename, 0, strrpos($filename, '.')); // 无后缀的文件名
        if (!in_array($ext, $allowExts)) {
            return array('success' => false, 'message' => '文件格式不支持!');
        }
        $data['createdTime'] = now();
        $data['dirName'] = $dirname; // xcall_13_002
        $data['exTags'] = $type; // moh
        $data['siteID'] = $siteID;
        $data['fileName'] = 'wav_' . $fnotext . '.wav';
        $data['filePath'] = $dir . '/wav_' . $fnotext . '.wav';
        $msg = sofunc_upload_handle($dir, array('exData' => $data, 'allowExts' => $allowExts, 'saveRule' => 'orig_' . $fnotext));
        if (!is_array($msg)) {
            return array('success' => false, 'message' => $msg);
        }
        return array('success' => false, 'message' => $msg['filePath'], 'data' => $msg);
    }

}
